<?php

namespace app\adminson\controller;
use app\common\model\NodeSon;
use think\Controller;

class Base extends Controller
{
    /**
     * page当前页码
     * @var string
     */
    public $page = '';
    /**
     * 每页显示多少条
     * @var string
     */
    public $size = '';
    /**
     * 查询条件的起始值
     * @var int
     */
    public $from = 0;


    public function _initialize()
    {
        //判断会话是否过期
        if (time() - session('session_start_time') > config('session.expire')) {
            session(null);
            session(null, config('admin.session_adminuser_scope'));//清除登录标识
        }
        $comid = session('comid');
        //判断是否登录
        if($this->isLogin() === false) {
            json(array("code"=>"-1","msg"=>"请先登录"))->send();die;
        }
        if(empty($comid)){
            json(array("code"=>"-2","msg"=>"缺少参数"))->send();die;
        }

        $manager = db('company')->field('manager_id')->where(['id'=>$comid])->find();
        $module     = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $action     = strtolower(request()->action());
        $url        = $module."/".$controller."/".$action;

        if( $manager['manager_id'] != session('uid')){
            if(!in_array($url, config('admin.permission_authlist')))
            {
                if(!$this->check($url,session('uid')))
                {
                    json(array("code"=>"-2","msg"=>"抱歉，您没有操作权限!"))->send();die;
                }
            }
        }

        $node = new NodeSon();

        $this->assign([
            'username' => session('username'),  //用户名
            'rolename' => session('rolename'),  //角色名
            'menu' => $node->getMenu(session('rule'))   //session('rule')权限列表ID
        ]);
    }

    /**
     * [isLogin  判定是否登录]
     * @author [C。]
     * @return bool
     */
    public function isLogin() {
        //获取session
        $adminuser = session(config('admin.session_adminuser'), '', config('admin.session_adminuser_scope'));
        if($adminuser && $adminuser['id']) {
            return true;
        }
        return false;
    }


    /**
     * [getPageAndSize  获取分页page size 内容]
     * @author [C。]
     * @param $data
     */
    public function getPageAndSize($data) {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty(config('list_rows')) ? config('list_rows') : config('paginate.list_rows');//后台设置显示条数、配置文件显示条数
        $this->from = ($this->page - 1) * $this->size;
    }

    /**
     * [check  检测是否有权限]
     * @author [C。]
     * @param $data
     */
    public function check($url,$id) {
        echo $url;die;
    }
}